package com.edu.demo.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
 *
 *   @Author: SJY
 *   @Date: 2024/3/6-03-06-19:18
 *   统一返回格式、统一返回结果
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result<T> {
    @ApiModelProperty(value = "状态码，请参考状态码手册")
    private Integer code;
    @ApiModelProperty(value = "响应信息")
    private String msg;
    @ApiModelProperty(value = "响应结果")
    private T data;

    //如果data为空就不渲染到json里面
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty(value = "响应结果")
    public Result code(Integer code) {
        this.code = code;
        return this;
    }

    public Result msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result data(T data){
        this.data = data;
        return this;
    }


    //构造函数
    public Result(Integer code) {
        this.code = code;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    /**
     * 请求成功
     *
     * @return
     */
    public static Result success() {
        return new Result(200);
    }

    public static Result success(Object data) {
        return new Result(200, data);
    }

    public static Result success(String msg) {
        return new Result(200, msg);
    }

    /**
     * 请求失败
     *
     * @return
     */
    public static Result error() {
        return new Result(500);
    }


    public static Result error(Integer code, String msg) {
        return new Result(500, msg);
    }

    public static Result error(Integer code, String msg, Object data) {
        return new Result(code, msg, data);
    }
}
